package com.store.app.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.adapter.HPTDetialsAdapter;
import com.store.app.bean.HPTDetailsBean;
import com.store.app.imlife.utils.ToastTools;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/19.
 * 预售详情页面
 */
public class HPTDetailsActivity extends BaseActivity implements IOnHttpListener {
    private NetRequest netRequest;
    private TextView tvTitle;
    private ImageView public_ll_return;
    private ListView lv;
    private List<HPTDetailsBean> hpyDetailsBeans;
    private HPTDetialsAdapter hptDetialsAdapter;
    private String parent_order_id;
    private TextView tv_state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hpt_details);
        parent_order_id = getIntent().getStringExtra("parent_order_id");
        initView();
        initData();


    }

    private void initData() {
        netRequest = new NetRequest(this);
        showProgressDialog("","正在加载详情页面...");
        netRequest.requestHTPOrderDetails(1, parent_order_id);
    }

    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            try {
                Log.v("zyl", "result:" + result);
                JSONObject jsonObjectResult = new JSONObject(result);
                JSONArray jsonArray = jsonObjectResult.getJSONArray("list");
                Gson gson = new Gson();
                hpyDetailsBeans.clear();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Log.v("zyl", "jsonObject:" + jsonObject.toString());
                    HPTDetailsBean hptDetailsBean = gson.fromJson(jsonObject.toString(), HPTDetailsBean.class);
                    Log.v("zyl", "hptDetailsBean是否为空:" + (hptDetailsBean == null));
                    hpyDetailsBeans.add(hptDetailsBean);
                }
                if(hpyDetailsBeans.size()==0){
                    tv_state.setVisibility(View.VISIBLE);
                }else{
                    tv_state.setVisibility(View.GONE);
                }
                hptDetialsAdapter.notifyDataSetChanged();
                Log.v("zyl", "hpyDetailsBeans:" + hpyDetailsBeans.size());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        ToastTools.showShort(HPTDetailsActivity.this, msg);
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("详情");
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        lv = (ListView) findViewById(R.id.lv);
        hpyDetailsBeans = new ArrayList<>();
        hptDetialsAdapter = new HPTDetialsAdapter(hpyDetailsBeans, this);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lv.setAdapter(hptDetialsAdapter);
        tv_state = (TextView) findViewById(R.id.tv_state);
    }
}
